<?php

namespace app\admin\services;

use app\admin\model\CustomerModel;
use think\facade\Validate;

class CustomerService extends BaseService
{
    public static function index($p){
        $params=[
            'page'=>($p['pageNo'])??1,
            'pageSize'=>($p['pageSize'])??10,
            'where'=>[],
        ];
        if(!empty($p['keyword']))
            $params['where'][]=['label|short_label','like',"%".$p['keyword']."%"];
        if(!empty($p['relationship']))
            $params['where'][]=['relationship','like',"%".($p['relationship'])."%"];
        $list=CustomerModel::getList($params);
        $count=CustomerModel::count($params['where']);
        api_return(200,'',['list'=>$list,'total'=>$count]);
    }
    public static function edit($p){
        $rule=[
            'label'=>'require',
            'short_label'=>'require',
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($p))
            api_return(500,$validate->getError());
        if(!empty($p['tempPath'])&&!empty($p['fileType'])) {
            $p['file_path']=self::getUploadField($p['tempPath'],$p['fileType'],($p['temp_old'])??null);
        }
        CustomerModel::edit($p);
    }
    public static function del($p){
        $rule=[
            'ids'=>'require',
        ];
        $validate=Validate::rule($rule);
        if(!$validate->check($p))
            api_return(500,'',$validate->getError());
        CustomerModel::delByIds($p['ids']);
    }

}
